/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2008 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace Galaxium.Protocol.GaduGadu
{
	public class ReceiveMessageCommand : GaduCommand
    {
        private int _sender;
        private int _sequence;
        private int _time;
        private int _class;
        private byte[] _message;
        
        public int Sender
        {
            get { return _sender; }
        }
        
        public int Sequence
        {
            get { return _sequence; }
        }
        
        public int Time
        {
            get { return _time; }
        }
        
        public int Class
        {
            get { return _class; }
        }
        
        public String Message
        {
            get
            {
                Encoding enc = Encoding.Unicode;
                return enc.GetString(Encoding.Convert(Encoding.GetEncoding("windows-1250"), Encoding.Unicode, _message));
            }
			set
			{
				_message = new byte[value.Length + 1];
				
                Encoding enc = Encoding.GetEncoding("windows-1250");
                byte[] cp1250;
                cp1250 = enc.GetBytes(value);
				
                for (int i = 0; i < value.Length; i++)
                    _message[i] = cp1250[i];
				
                _message[value.Length] = (byte)'\0';
			}
        }
        
        public ReceiveMessageCommand () : base (GaduConstants.CMD_IN_RECV_MSG)
        {
        	
		}
		
        protected override void Read (BinaryReader reader)
        {
            _sender = reader.ReadInt32 ();
            _sequence = reader.ReadInt32 ();
            _time = reader.ReadInt32 ();
            _class = reader.ReadInt32 ();
            
            _message = ReadString (reader);
        }
        
        public override string ToString ()
        {
        	return String.Empty;
        }
    }
}